package com.ruoyi.project.hcy.notice.controller;

import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.hcy.notice.domain.NoticeAndType;
import com.ruoyi.project.hcy.notice.domain.NoticeSeeCount;
import com.ruoyi.project.hcy.notice.domain.NoticeType;
import com.ruoyi.project.hcy.notice.domain.Notices;
import com.ruoyi.project.hcy.notice.mapper.NoticeAndTypeMapper;
import com.ruoyi.project.hcy.notice.service.INoticeManagerService;
import com.ruoyi.project.hcy.notice.service.INoticeSeeCountService;
import com.ruoyi.project.hcy.notice.service.INoticeTypeService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("notice/manager")
public class NoticeManagerController extends BaseController {

    @Autowired
    private INoticeSeeCountService noticeSeeCountService;

    @Autowired
    private INoticeTypeService noticeTypeService;

    @Autowired
    private IUserService userService;

    @Autowired
    private INoticeManagerService noticeManagerService;

    @Autowired
    private NoticeAndTypeMapper noticeAndTypeMapper;

    private String prefix="hcy/notice";

    /**
     * 跳转公告管理页面
     * @return
     */
    @RequiresPermissions("notice:manager:view")
    @GetMapping()
    public String notices()
    {

        return prefix + "/notices";
    }

    @RequiresPermissions("notice:manager:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(Notices notices){
        startPage();
        List<Notices> list = noticeManagerService.selectNoticesList(notices);
        return getDataTable(list);
    }

    @GetMapping("/add")
    public String add(Model mmap)
    {
        //获取公告类型
        List<NoticeType> noticeTypes = noticeTypeService.selectNoticeTypeList(null);
        mmap.addAttribute("list",noticeTypes);
        return prefix + "/noticesAdd";
    }

    @RequiresPermissions("notice:manager:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Notices notices, Integer typeId){

        //更新公告类型和公告的连接表  公告id和公告类型id
        notices.setUserId(getSysUser().getUserId());
        int i = noticeManagerService.insertNotices(notices);
        System.out.println(notices.getNoticeId());
        System.out.println(typeId);
        int a=noticeManagerService.updateNoticeAndType(typeId,notices.getNoticeId());

        return toAjax(i);
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @RequiresPermissions("notice:manager:remove")
//    @Log(title = "岗位管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {

        try
        {
            return toAjax(noticeManagerService.deleteNoticesByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }


    /**
     * 修改岗位
     */
    @GetMapping("/edit/{noticeId}")
    public String edit(@PathVariable("noticeId") Integer noticeId, ModelMap mmap)
    {
        mmap.put("notices", noticeManagerService.selectNoticesById(noticeId));
        return prefix + "/noticesEdit";
    }
    /**
     * 修改保存岗位
     */
    @RequiresPermissions("notice:manager:edit")
//    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Notices notices)
    {
        return toAjax(noticeManagerService.updateNotices(notices));
    }


    @RequiresPermissions("notice:manager:look")
//    @Log(title = "岗位管理", businessType = BusinessType.UPDATE)
    @GetMapping("/look")

    public String look(Integer noticeId,ModelMap mmap)
    {
        System.out.println(noticeId);
        Notices notices = noticeManagerService.selectNoticesById(noticeId);
        mmap.put("notices", notices);
        Long userId = notices.getUserId();
        User user = userService.selectUserById(userId);
        mmap.put("userName", user.getLoginName());

        //每调用一次，统计表加1  记录最后一次浏览时间 判断数据库有没有这条数据，没有就插入，有就更新（id）

        NoticeSeeCount noticeSeeCount = noticeSeeCountService.selectNoticeSeeCountById(noticeId);
        //得到typeId
        NoticeAndType noticeAndType = noticeAndTypeMapper.selectNoticeAndTypeById(noticeId);
        //组装NoticeSeeCount
        NoticeSeeCount noticeSeeCount1 = new NoticeSeeCount();
        //公告id
        noticeSeeCount1.setNoticeId(noticeId);
        //类型id
        noticeSeeCount1.setTypeId(noticeAndType.getTypeId());
        //当前时间
        noticeSeeCount1.setSeeTime(new Date());

        //公告名称
        noticeSeeCount1.setNoticeName(notices.getNoticeName());
        //第一次被查看
        if(noticeSeeCount==null){
            noticeSeeCount1.setSeeCount(1l);
            noticeSeeCountService.insertNoticeSeeCount(noticeSeeCount1);
        }else{
            long a=noticeSeeCount.getSeeCount()+1;
            noticeSeeCount1.setSeeCount(a);
            noticeSeeCountService.updateNoticeSeeCount(noticeSeeCount1);
        }


        return prefix + "/noticesLook";
    }

}
